<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>${SYSTEM_NAME}</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layuiadmin/style/admin.css" media="all">
  <link rel="stylesheet" href="${pageContext.request.contextPath}/static/js/zTree_v3/css/metroStyle/metroStyle.css?v=${FILE_VERSION}" type="text/css">
</head>
<body>
<input type="text" id="roleId" style="display: none;"  value="${roleId }"/>
<div style="border: 1px solid #ccc; max-height: 480px;min-height:400px;overflow: auto;" >
 	<ul id="treeRegion" class="ztree">
        菜单加载中....

    </ul>
 </div>
 <div class="layui-input-block" style="padding-top: 10px">
      <button class="layui-btn" type="button" onclick="roleMenu()">设置</button>
      <button type="button" class="layui-btn layui-btn-danger" onclick="closed()">关闭</button>
</div>

<script src="${pageContext.request.contextPath}/static/js/common-2.0.0.js?v=${FILE_VERSION}"></script>
<script src="${pageContext.request.contextPath}/static/layuiadmin/layui/layui.js?v=${FILE_VERSION}"></script>
<script src="${pageContext.request.contextPath}/static/js/jquery-3.3.1.min.js?v=${FILE_VERSION}"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/zTree_v3/js/jquery.ztree.core.js?v=${FILE_VERSION}"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/zTree_v3/js/jquery.ztree.excheck.js?v=${FILE_VERSION}"></script>
<script>
    var layer;
    layui.use(['layer'], function() {
        layer = layui.layer;
    });
var ZtreeObj;

var updateTableCell;
var setting = {
	check: {
		enable: true
	},
	data: {
		simpleData: {
			enable: true
		}
	},
	edit: {
		enable: false
	},
	callback : {
		onClick: treeOnClick 
	}
};

function treeOnClick(event, treeId, treeNode) {
	parentName = treeNode.name;
	reload(treeNode.id);
}

$(document).ready(function(){
	$.ajax({
	    url : getContextPath() + '/sys/role/add/menuAuthority',
	    data: {
			"roleId" : $("#roleId").val()
        },
	    type : "post",
	    dataType : "text",
	    success : function(data) {
	    	var zNodes = eval("(" + data + ")");
	    	ZtreeObj = $.fn.zTree.init($("#treeRegion"), setting, zNodes);
	    },
	    error : function(XMLHttpRequest, textStatus) {
	    	layer.msg(textStatus);
	    }
	});
});

function roleMenu(){
	var treeObj = $.fn.zTree.getZTreeObj("treeRegion");
	var nodes = treeObj.getCheckedNodes(true);
	var ids = "";
	for(var i=0; i<nodes.length; i++){
		ids += nodes[i].id + ",";
	}
	if(nodes.length != 0){
		ids = ids.substr(0,ids.length-1);
	}

	$ajax({
		url: "/sys/role/add/setMenuRole?extendAll=true",
		data: {
			"roleId" : $("#roleId").val(),
        	"menuIds" : ids
        },
        succCallback: function (json) {
        	if(json.code == 0){
        		var index = parent.layer.getFrameIndex(window.name); //获取窗口索引  
        		parent.layer.close(index);//关闭弹出的子页面窗口
        		parent.layer.msg(json.msg);
        	}
        }
	});
}


function closed(){
	var index = parent.layer.getFrameIndex(window.name); //获取窗口索引  
	parent.layer.close(index);//关闭弹出的子页面窗口
}
</script>
</body>
</html>